//获取应用实例
const app = getApp();

Page({
	data: {
    isChecked2: true,
    nameId: '',
    phoneId: '',
    addressdetailId: '',
    id:'',
    addressMsg:[],
    consignee:'',
    phoneId:'',
    isEdit:false,
    // 省市区三级联动初始化
    region: ["", "", ""],
    province:'',
    city:'',
    county:'',
  },
	onLoad: function(option) {
    this.setData({
        id:option.id
    })

    console.log(this.data.id)
    if (this.data.id !=undefined){
      this.setData({
        isEdit:true
      })
      //查询地址信息
      this.getAddressList(this.data.id);
    }
    
  },

  // 选择省市区函数
  changeRegin(e) {
    this.setData({ region: e.detail.value });
    console.log(this.data.region)
    this.setData({
      province:this.data.region[0],
      city: this.data.region[1],
      county: this.data.region[2]
    })
  },

  //获取收货地址信息
  getAddressList(id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=ConsigneeAddress&a=getAddressMsg',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        'token': '' + app.globalData.token,
        'address_id':id,
      },
      success: (res) => {
        console.log(res)
        if (res.data.data.addressMsg.is_default=="Y"){
          that.setData({
            isChecked2: true
          })
        }else{
          that.setData({
            isChecked2: false
          })
        }
        
        that.setData({
          nameId: res.data.data.addressMsg.consignee,
          phoneId: res.data.data.addressMsg.contact_number,
          addressdetailId: res.data.data.addressMsg.detail_address,
          region: ["" + res.data.data.addressMsg.province, "" + res.data.data.addressMsg.city, "" + res.data.data.addressMsg.county]
        })
      },
    });
  },

  nameInput: function (e) {
    var that = this;
    that.setData({
      nameId: e.detail.value
    })
  },

  phoneInput: function (e) {
    var that = this;
    that.setData({
      phoneId: e.detail.value
    })
  },
  addressdetailInput: function (e) {
    var that = this;
    that.setData({
      addressdetailId: e.detail.value
    })
  },

  changeSwitch2: function (e) {
    // console.log('switch2 发生 change 事件，携带值为', e.detail.value)
    if(e.detail.value==true){
      this.setData({
        isChecked2:true
      })
    }else{
      this.setData({
        isChecked2: false
      })
    }
  },

  //添加收货地址
  addAddress() {
    var that = this;
    var isDefault='';

    if (that.data.nameId.length==0){
      wx.showToast({
        title: '请填写收货人',
        icon:'none'
      })
      return;
    }
    if (that.data.phoneId.length == 0) {
      wx.showToast({
        title: '请填写手机号码',
        icon: 'none'
      })
      return;
    }
    if (that.data.province.length == 0) {
      wx.showToast({
        title: '请选择省市区',
        icon: 'none'
      })
      return;
    }
    if (that.data.addressdetailId.length == 0) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      return;
    }

    if (that.data.isChecked2) {
      isDefault='Y';
    }else{
      isDefault='N'
    }
    console.log(app.globalData.token)
    console.log(that.data.province)
    console.log(that.data.city)
    console.log(that.data.county)
    console.log(that.data.consignee)
    console.log(that.data.contact_number)
    wx.request({
      url: app.globalData.api + '/app.php?c=ConsigneeAddress&a=addAddress',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        token: '' + app.globalData.token,
        province: ''+that.data.province,//省
        city: '' + that.data.city,//市
        county: '' + that.data.county,//区
        detail_address: '' + that.data.addressdetailId,//详细地址
        company:'',
        consignee: ''+that.data.nameId,//收件人
        contact_number:''+that.data.phoneId,//手机号码
        is_default: '' + isDefault,//是否为默认地址 Y是 N否
      },
      method: 'post',
      dataType: 'json',
      success: (res) => {
        console.log(res)
        if(res.data.code==0){
          wx.navigateBack({
            delta: 1
          })
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }else{
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
      },
    });
  },

  //编辑收货地址
  editAddress:function(e) {
    console.log(e.currentTarget.dataset.id)
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/ConsigneeAddress/edit',
      method: 'post',
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        token: '' + app.globalData.token,
        address_id: '' + e.currentTarget.dataset.id,
        province: '' + that.data.province,//省
        city: '' + that.data.city,//市
        county: '' + that.data.county,//区
        detail_address: '' + that.data.addressdetailId,//详细地址
        company: '',
        consignee: '' + that.data.nameId,//收件人
        contact_number: '' + that.data.phoneId,//手机号码
        postcode:'',
        is_default: 'N',//是否为默认地址 Y是 N否
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          if(that.data.isChecked2){
            that.select(e.currentTarget.dataset.id);
          }
        }
        wx.showToast({
          title: '' + res.data.msg,//提示文字
          duration: 2000,//显示时长
          mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon: 'success', //图标，支持"success"、"loading"  
          success: function () { },//接口调用成功
          fail: function () { },  //接口调用失败的回调函数  
          complete: function () { } //接口调用结束的回调函数  
        })
      },
    });
  },
  //修改默认收获地址
  select: function (addressid) {
    var ids = addressid
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/ConsigneeAddress/changeDefault',
      method: 'post',
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        token: '' + app.globalData.token,
        address_id: ids,
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
      },
    });
  },
});
